package tk0084.dev.google.quiz;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	/**
	 * @param args
	 * @throws IOException
	 * @throws ClassNotFoundException
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 */
	public static void main(String[] args) throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException {

		BufferedReader br = new BufferedReader(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("data.txt")));
		String[] header = br.readLine().split(" ");
		final int LX = Integer.parseInt(header[0]);
		final int RX = Integer.parseInt(header[1]);
		final int UX = Integer.parseInt(header[2]);
		final int DX = Integer.parseInt(header[3]);

		int puzzleCount = Integer.parseInt(br.readLine());

		String line = null;
		int count = 1;
		while ((line = br.readLine()) != null) {
			String[] info = line.split(",");
			if (info.length != 3) {
				return;
			}

			int row = Integer.parseInt(info[0]);
			int col = Integer.parseInt(info[1]);
			String chars = info[2];

			Puzzle puzzle = new Puzzle(chars, row, col);

			if (args[0] != null) {
				PuzzleStrategy strategy = (PuzzleStrategy) Class.forName(args[0]).newInstance();
				puzzle.setStrategy(strategy);
			}

			State endState = puzzle.start();

			if (endState != null) {
				System.out.println(count + "\t" +endState.printHistory());
			} else {
				System.out.println(count);
			}
			
//			if (puzzle.getRow() * puzzle.getCol() < 15) {
//				State endState = puzzle.start();
//
//				if (endState != null) {
//					System.out.println(count + "\t" +endState.printHistory());
//				} else {
//					System.out.println(count);
//				}
//			} else {
//				System.out.println(count);
//			}
			
			count++;
		}
	}

}
